<?php
namespace Home\Controller;
use Think\Controller;
class AppController extends Controller {
	 public function _initialize(){
		$this->assign('index',5);
   }
   
	public function login(){
		$Model = M();
		
		$code =$_POST["code"];
		$mac =$_POST["mac"];
		$danwei=M('danwei');
		$fenzu=M('fenzu');
		$app=M('app');
		
		$appinfo = $app->where("")->find();
		
		$obj =$danwei->where("  code='".$code."'")->find();
		if($obj){
			$ss["state"]=$obj["state"];
			$fenzuinfo =$fenzu->where("id=".$obj["zuid"])->find();
			$obj["fenzuname"]=$fenzuinfo["zuname"];
			$obj["appname"]=$appinfo;
			$ss["obj"]=$obj;
			$Model->execute("update danwei set mac='".$mac."' where code='".$code."'");
			
		}else{
			$ss["message"]="编码不存在";
			$ss["state"]="-1";
			
		}
		$this->ajaxReturn($ss,'JSON');
		
		
    }
	
	public function gettongzhi(){
		$Model = M();
		$obj =$Model->query("select  * from tongzhi where typename='滚屏' order by createtime desc limit 0,1");
		$this->ajaxReturn($obj[0],'JSON');
	}
	public function getmenu(){
		$menu=M('menu');
		$Model = M();
		$obj =$Model->query("select  * from tongzhi where typename='滚屏' order by createtime desc limit 0,1");
		$list =$menu->where("")->select();
		foreach($list as $kk=>$value){
			$value["imgurl"]=$_SERVER['HTTP_HOST'].$value["imgurl"];
			
		}
		$ss["menu"]= $list;
		$ss["tongzhi"]=$obj;
		$this->ajaxReturn($ss,'JSON');
	}
	public function gettv(){
		$tv=M('tv');
		$tvlist =$tv->where("typeid=2")->order("seq asc")->select();
		$this->ajaxReturn($tvlist,'JSON');
	}
	public function addlog(){
		$log=M('log');
		$data["content"]=$_POST["content"];
		$data["code"]=$_POST["code"];
		$data["createtime"]=time();
		$data["typeid"]=$_POST["typeid"];
		$log->add($data);
	}
	public function kaihuicheck(){
		$tv=M('tv');
		$zuid=$_POST["zuid"];

		$tvlist =$tv->where("typeid=1 and state=1 and kaihuizuid=".$zuid." or (typeid=1 and state=1 and kaihuizuid='0') ")->order("typeid asc")->find();
		if($tvlist){
			$obj["obj"]=$tvlist;
			$obj["state"]=1;
			$this->ajaxReturn($obj,'JSON');
			
		}else{
			$obj["obj"]="";
			$obj["state"]=0;
			$this->ajaxReturn($obj,'JSON');
			
		}
		
		
	}
	public function version(){
		
		$str= file_get_contents("app.txt");
		$items  = explode("|",$str);
		$obj["version"]=$items[0];
		$obj["url"]="http://".$_SERVER['HTTP_HOST']."/".$items[1];
		
		$this->ajaxReturn($obj,'JSON');
		
	}
	
	public function getvideos(){
		header("Content-Type:text/html;charset=utf-8");
		$dir="./videos".$_GET["filename"]."/";
		$dir =iconv('utf-8' , 'gbk' , $dir );

		$f=scandir($dir);
		
		$arr = Array();
		foreach($f as $file) {
		$name =iconv('gbk' , 'utf-8' , $file );
		// $name = $file;
		// echo $dir.$file;
		  if(is_dir($dir.$file)){
			  $obj["name"]=$name;
			  $obj["type"]="1";
		  }else{
			  $obj["name"]=$name;
			  $obj["type"]="0";
		  }
		
		  if(strpos($name,".jpg")>-1){
			 
		  }else if($name=="."){
			  
			  
		  }else if($name==".."){
			  
			  
		  }else{
			   array_push($arr,$obj);
		  }
		}
		$arr=array_diff($arr,array('.','..'));

		echo json_encode($arr);
		
	}

	public function getapp(){
		
		$app=M('app');
		$info = $app->where("")->find();
		
		$this->ajaxReturn($info,'JSON');
		
	}
	
	public function getfengcai(){
		$news=M('news');
		$list =$news->where("")->order("id desc")->limit(0,20)->select();
		foreach ($list as $key => &$value) {
			$value["createtime"]=date("Y-m-d H:i",$value["createtime"]); 
		
		}
		$this->ajaxReturn($list,'JSON');
		
	}
	public function fengcaiview(){
		$news=M('news');
		$info =$news->where("id=".$_GET["id"])->find();
		$this->assign('info',$info);
		$this->display();
		
	}
	


}